﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Configuration;
using System.Data;

namespace NegevGas.Core
{
    public class HtzFile
    {
        // Methods
        public static HtzFile AddFile(string projectName, string mapName, string featureSource, long uid, string fileName, string description)
    {
        DBUploadFiles _db = new DBUploadFiles(projectName);
        if (!_db.IsExists(fileName, featureSource, uid))
        {
            HtzFile f = new HtzFile {
                UID = uid,
                Description = description,
                FeatureSource = featureSource,
                FeatureSourceId = (int) _db.GetFeatureSourceId(featureSource, false),
                FileName = fileName,
                MapName = mapName,
                UploadDate = DateTime.Now.ToString("dd/MM/yyyy"),
                ProjectName = projectName
            };
           
            if (_db.AddFile(fileName, description, mapName, featureSource, uid))
            {
                f.ID = _db.GetLastInsertedId();
                f.FeatureSourceId = (int) _db.GetFeatureSourceId(featureSource, false);
                return f;
            }
        }
        return null;
    }

        public static string DeleteFile(string projectName, long id)
        {
            DBUploadFiles _db = new DBUploadFiles(projectName);
            return _db.DeleteFile(id);
        }

        public static HtzFile[] GetFiles(string projectName, string featureSource, long uid)
        {
            List<HtzFile> result = new List<HtzFile>();
            DBUploadFiles _db = new DBUploadFiles(projectName);
            IDataReader reader = _db.GetFiles(featureSource, uid);
            while (reader.Read())
            {
                HtzFile file = new HtzFile
                {
                    UID = uid,
                    FeatureSource = featureSource,
                    FeatureSourceId = (int)_db.GetFeatureSourceId(featureSource, false),
                    ID = (long)reader["Id"],
                    MapName = reader["MapName"].ToString(),
                    FileName = reader["FileName"].ToString(),
                    Description = reader["Description"].ToString()
                };
                file.UploadDate = ((DateTime)reader["UploadDate"]).ToString("dd/MM/yyyy");
                file.ProjectName = projectName;
                result.Add(file);
            }
            return result.ToArray();
        }

        public static bool UpdateFile(string projectName, long id, string description)
        {
            DBUploadFiles _db = new DBUploadFiles(projectName);
            return _db.UpdateFile(id, description);
        }

        // Properties
        public string Description { get; set; }

        public string FeatureSource { get; set; }

        public int FeatureSourceId { get; set; }

        public string FileIcon
        {
            get
            {
                return string.Format("{0}.png", Path.GetExtension(this.FileName)).Trim(new char[] { '.' });
            }
        }

        public string FileName { get; set; }

        public string FileUrl
        {
            get
            {
                return VirtualPathUtility.ToAbsolute(string.Format("~/Uploads/{0}/{1}/{2}/{3}", new object[] { this.ProjectName, this.FeatureSourceId, this.UID, this.FileName }));
            }
        }

        public long ID { get; set; }

        public string MapName { get; set; }

        public string PathToFile
        {
            get
            {
                return ConfigurationManager.AppSettings["FilesRepositoryupload"]+string.Format(@"{0}\{1}\{2}\{3}", new object[] { this.ProjectName, this.FeatureSourceId, this.UID, this.FileName });
            }
        }

        public string ProjectName { get; set; }

        public long UID { get; set; }

        public string UploadDate { get; set; }
    }


}